package cn.tedu.travelsystem.participation.controller;

import cn.tedu.travelsystem.base.response.JsonResult;
import cn.tedu.travelsystem.carpool.mapper.CarPoolMapper;
import cn.tedu.travelsystem.carpool.pojo.entity.Carpool;
import cn.tedu.travelsystem.participation.mapper.ParticipationMapper;
import cn.tedu.travelsystem.participation.pojo.dto.ParticipationParm;
import cn.tedu.travelsystem.participation.pojo.entity.Participation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/participation")
@Api(tags = "02.拼单参与模块")
public class ParticipationController {
//    @Autowired
//    private ParticipationMapper participationMapper;
//
//    @Autowired
//    private CarPoolMapper carPoolMapper; // 注入CarPoolMapper
//
//
//    @PostMapping("addP")
//    @ApiOperation("增加拼车参与信息")
//    @ApiOperationSupport(order = 10)
//    public JsonResult addP(@Validated @RequestBody ParticipationParm participationParm) {
//        log.debug("participationParm = " + participationParm);
//
//        Participation participation = new Participation();
//        BeanUtils.copyProperties(participationParm, participation);
//        participation.setJoinTime(new Date());
//
//        participationMapper.insertParticipation(participation);
//
//        // 更新座位信息
//        Long carpoolId = participation.getCarpoolId();
//        Carpool carpool = carPoolMapper.getById(carpoolId);
//
//        if (carpool != null && carpool.getSeatsAvailable() > 0) {
//            // 创建一个新的Carpool对象来更新座位信息
//            Carpool updatedCarpool = new Carpool();
//            updatedCarpool.setCarpoolId(carpoolId);
//            updatedCarpool.setSeatsAvailable(carpool.getSeatsAvailable() - 1);
//            updatedCarpool.setCurrentParticipants(carpool.getCurrentParticipants() + 1);
//            updatedCarpool.setVersion(carpool.getVersion()); // 获取当前版本号
//
//            int rows = carPoolMapper.updateSeats(updatedCarpool); // 传递Carpool对象
//            if (rows > 0) {
//                return JsonResult.ok();
//            } else {
//                return JsonResult.error("座位信息更新失败");
//            }
//        }
//
//        return JsonResult.error("拼车信息不存在或座位已售罄");
//    }
//    @DeleteMapping("deleteP/{participationId}")
//    @ApiOperation("删除拼车参与信息")
//    @ApiOperationSupport(order = 50)
//    @ResponseBody
//    public String delete(@PathVariable Long participationId) {
//        log.debug("Deleting participation with ID = " + participationId);
//
//        Participation participation = participationMapper.getById(participationId);
//
//        if (participation != null) {
//            int rows = participationMapper.deleteParticipation(participationId);
//
//            Long carpoolId = participation.getCarpoolId();
//            Carpool carpool = carPoolMapper.getById(carpoolId); // 直接使用CarPoolMapper
//
//            if (carpool != null && carpool.getCurrentParticipants() > 0) {
//                // 创建一个新的Carpool对象来更新座位信息
//                Carpool updatedCarpool = new Carpool();
//                updatedCarpool.setCarpoolId(carpoolId);
//                updatedCarpool.setSeatsAvailable(carpool.getSeatsAvailable() + 1);
//                updatedCarpool.setCurrentParticipants(carpool.getCurrentParticipants() - 1);
//                updatedCarpool.setVersion(carpool.getVersion()); // 获取当前版本号
//
//                carPoolMapper.updateSeats(updatedCarpool); // 传递Carpool对象
//            }
//
//            if (rows > 0) {
//                return "删除成功";
//            }
//        }
//
//        return "删除失败，参与信息不存在或已被删除";
//    }
//
//    @GetMapping("detailP/{participationId}")
//    @ApiOperation("根据ID查询拼车参与信息")
//    @ApiOperationSupport(order = 15)
//    @ResponseBody
//    public Participation getById(@PathVariable Long participationId) {
//        return participationMapper.getById(participationId);
//    }
//
//    @PutMapping("updateP/{participationId}")
//    @ApiOperation("根据ID修改拼车参与信息")
//    @ApiOperationSupport(order = 20)
//    @ResponseBody
//    public String updateById(@PathVariable Long participationId, @RequestBody Participation participation) {
//        participation.setParticipationId(participationId);
//        int rows = participationMapper.updateParticipationById(participation);
//        return rows > 0 ? "修改成功" : "修改失败，ID不存在";
//    }
}